package com.cqupt.scanning.system.file.helper;

import com.alibaba.fastjson.JSON;
import com.cqupt.scanning.handler.exception.MyException;
import com.cqupt.scanning.system.admin.entity.LandmarkCoordinatesThree;
import com.cqupt.scanning.system.admin.entity.PatientThree;
import com.cqupt.scanning.system.file.entity.bo.ChildBo;
import com.cqupt.scanning.system.file.entity.bo.MesurementBO;
import com.csvreader.CsvReader;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析特定文件格式类
 * @Data: 2021/7/7 10:26
 * @Author: 宋宝梁
 */
public class ReadUploadedFile {

    /**
     * 解析足底json文件
     * @param file
     * @param t
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T analysisJsonFile(File file, T t, Class<T> clazz) {
        InputStream is = null;
        String jsonText;
        try {
            is = new FileInputStream(file);
            if (file.getName().contains("userinfo")) {
                String content = IOUtils.toString(is, StandardCharsets.UTF_8);
                int index = content.indexOf('}');
                jsonText = content.substring(0, index + 1);
                t = JSON.parseObject(jsonText, clazz);
                return t;
            }
            jsonText = IOUtils.toString(is, StandardCharsets.UTF_8);
            t = JSON.parseObject(jsonText, clazz);
            return t;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is!=null) is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return t;
    }

    /**
     * 解析足底csv文件
     * @param file
     * @param list
     */
    public static void analysisCsvFile(File file, List<String[]> list) {
        Reader reader = null;
        CsvReader csvReader = null;
        try {
            reader = new FileReader(file);
            csvReader = new CsvReader(reader);
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                list.add(csvReader.getValues());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (csvReader!=null) csvReader.close();
                if (reader!=null) reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 解析三维csv文件
     * @param child
     * @return
     */
    public static ChildBo analysisCsvFile(String child) {


        CsvReader reader = null;
        ChildBo childBo = new ChildBo();
        try {
            ArrayList<String[]> csvFileList = new ArrayList<>();
            reader = new CsvReader(child, ',', StandardCharsets.UTF_16LE);


            while (reader.readRecord()) csvFileList.add(reader.getValues());
            for (int row = 0; row < csvFileList.size(); row++) {
                // 取得第row行第0列的数据
                String cell = csvFileList.get(row)[0];
                String[] s = cell.split("\t");
                if (row == 0&&s.length == 1) {
                    childBo.setBaseInfo(s[0]);
                    continue;
                }
                if (s[0].equals("Business Name")) {
//                    childBo.setBusinessName(s[0]);
                    continue;
                }
                if (s[0].equals("Address"))  {
//                    childBo.setAddress(s[0]);
                    continue;
                }
                if (s[0].equals("City")) {
//                    childBo.setCityProvince(s[0]);
                    continue;
                }
                if (s[0].equals("Country"))  {
//                    childBo.setCountry(s[0]);
                    continue;
                }
                if (s[0].equals("Phone"))  {
//                    childBo.setCompanyPhone(s[0]);
                    continue;
                }
                if (s[0].equals("Fax")) {
//                    childBo.setFax(s[0]);
                    continue;
                }
                if (s[0].equals("Date of Birth")) {
                    if( s.length==2){
                        childBo.setDataOfBirth(s[1]);
                    }
                    continue;
                }
                if (s[0].equals("Age")) {
                    if( s.length==2){
                        childBo.setAge(s[1]);
                    }
                    continue;
                }
                if (s[0].equals("Height")) {
                    if(s.length==2){
                        childBo.setHeight(s[1]);
                    }
                    continue;
                }
                if (s[0].equals("Weight")) {
                    if(s.length==2){
                        childBo.setWeight(s[1]);
                    }
                    continue;
                }
                if (s[0].equals("Weight Bearing %")) {
                    childBo.setWeightBearing(s[1]);
                    continue;
                }
                if (s[0].equals("Dress Shoe"))  {
//                    childBo.setDressShoe(s[0]);
                    continue;
                }
                if (s[0].equals("Athletic Shoe"))  {
//                    childBo.setAthleticShoe(s[0]);
                    continue;
                }
                if (s[0].equals("Casual Shoe")) {
//                    childBo.setCasualShoe(s[0]);
                    continue;
                }
                if (s[0].equals("Heels")) {
//                    childBo.setHeels(s[0]);
                    continue;
                }

                if (s[0].equals("联系电话")||s[0].equals("电话")||s[0].equals("联系方式")||s[0].equals("phone")||s[0].equals("手机号码")||s[0].equals("Optional Input 1")) {
                    if(s.length==2){
                        childBo.setPhone(s[1]);
                    }
                    continue;
                }

                if (s[0].equals("Optional Input 2")) {
//                    childBo.setOptionalInputTwo(s[0]);
                    continue;
                }
                if (s[0].equals("Optional Input 3")) {
//                    childBo.setOptionalInputThree(s[0]);
                    continue;
                }
                if (s[0].equals("Optional Input 4")){
//                    childBo.setOptionalInputFour(s[0]);
                    continue;
                }
                if (s[0].equals("Optional Input 5")) {
//                    childBo.setOptionalInputFive(s[0]);
                    continue;
                }
                if (s[0].equals("Optional Input 6")) {
//                    childBo.setOptionalInputSix(s[0]);
                    continue;
                }
                if (s[0].equals("Optional Input 7")) {
//                    childBo.setOptionalInputSeven(s[0]);
                    continue;
                }
                if (s[0].equals("Optional Input 8")){
//                    childBo.setOptionalInputEight(s[0]);
                    continue;
                }
                if (s[0].equals("Optional Input 9")) {
//                    childBo.setOptionalInputNine(s[0]);
                    continue;
                }
                if (s[0].equals("Optional Input 10")) {
//                    childBo.setOptionalInputTen(s[0]);
                    continue;
                }

                if(s[0].equals("Length Measurements")){
                    for (int j=row+1; j < row+8; j++) {
                        // 取得第row行第0列的数据
                        String MeasurementsCell = csvFileList.get(j)[0];
                        String[] MeasurementsS = MeasurementsCell.split("\t");
                        if (MeasurementsS[0].equals("Foot")) {
                            childBo.setFootL(MeasurementsS[1]).setFootR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("Arch")) {
                            childBo.setArchL(MeasurementsS[1]).setArchR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("1 Met to Heel Tip")) {
                            childBo.setOneHeelTipL(MeasurementsS[1]).setOneHeelTipR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("5 Met to Heel Tip")) {
                            childBo.setFiveHeelTipL(MeasurementsS[1]).setFiveHeelTipR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("HC to Heel Tip")) {
                            childBo.setHcHeelTipL(MeasurementsS[1]).setHcHeelTipR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("Lat Arch to Heel Tip")) {
                            childBo.setLatArchHeelTipL(MeasurementsS[1]).setLatArchHeelTipR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("Med Arch to Heel Tip")) {
                            childBo.setMedArchHeelTipL(MeasurementsS[1]).setMedArchHeelTipR(MeasurementsS[2]);
                            continue;
                        }
                    }
                }
                if(s[0].equals("Width Measurements")){
                    for (int j=row+1; j < row+5; j++) {
                        // 取得第row行第0列的数据
                        String MeasurementsCell = csvFileList.get(j)[0];
                        String[] MeasurementsS = MeasurementsCell.split("\t");
                        if (MeasurementsS[0].equals("ForeFoot")) {
                            childBo.setForeFootL(MeasurementsS[1]).setForeFootR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("Heel")) {
                            childBo.setHeelL(MeasurementsS[1]).setHeelR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("Mid-Foot")) {
                            childBo.setMidFootL(MeasurementsS[1]).setMidFootR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("1-5 Met")) {
                            childBo.setOneToFiveMetL(MeasurementsS[1]).setOneToFiveMetR(MeasurementsS[2]);
                            continue;
                        }
                    }
                }

                if(s[0].equals("Height Measurements")){
                    for (int j=row+1; j < row+2; j++) {
                        // 取得第row行第0列的数据
                        String MeasurementsCell = csvFileList.get(j)[0];
                        String[] MeasurementsS = MeasurementsCell.split("\t");
                        if (MeasurementsS[0].equals("Arch")) {
                            childBo.setHeightArchL(MeasurementsS[1]).setHeightArchR(MeasurementsS[2]);
                            continue;
                        }
                    }
                }
                if(s[0].equals("Angle Measurements")){
                    for (int j=row+1; j < row+2; j++) {
                        // 取得第row行第0列的数据
                        String MeasurementsCell = csvFileList.get(j)[0];
                        String[] MeasurementsS = MeasurementsCell.split("\t");
                        if (MeasurementsS[0].equals("Toe 1")) {
                            childBo.setAngleToeOneL(MeasurementsS[1]).setAngleToeOneR(MeasurementsS[2]);
                            continue;
                        }
                    }
                }
                if(s[0].equals("Shoe Size (By Foot Length)")){
                    for (int j=row+1; j < row+2; j++) {
                        // 取得第row行第0列的数据
                        String MeasurementsCell = csvFileList.get(j)[0];
                        String[] MeasurementsS = MeasurementsCell.split("\t");
                        if (MeasurementsS[0].equals("Shoe>Foot 15mm")) {
                            childBo.setShoeSizeL(MeasurementsS[1]).setShoeSizeR(MeasurementsS[2]);
                            continue;
                        }
                    }
                }
                if(s[0].equals("Arch Height")){
                    for (int j=row+1; j < row+4; j++) {
                        // 取得第row行第0列的数据
                        String MeasurementsCell = csvFileList.get(j)[0];
                        String[] MeasurementsS = MeasurementsCell.split("\t");
                        if (MeasurementsS[0].equals("Medial Height")) {
                            childBo.setArchMedialHeightL(MeasurementsS[1]).setArchMedialHeightR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("Area Height")) {
                            childBo.setArchAreaHeightL(MeasurementsS[1]).setArchAreaHeightR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("Lateral Height")) {
                            childBo.setArchLateralHeightL(MeasurementsS[1]).setArchLateralHeightR(MeasurementsS[2]);
                            continue;
                        }
                    }
                }

                if(s[0].equals("Arch Index")){
                    for (int j=row+1; j < row+3; j++) {
                        // 取得第row行第0列的数据
                        String MeasurementsCell = csvFileList.get(j)[0];
                        String[] MeasurementsS = MeasurementsCell.split("\t");
                        if (MeasurementsS[0].equals("Arch Index")) {
                            childBo.setArchIndexL(MeasurementsS[1]).setArchIndexR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("Type"))  {
                            childBo.setArchIndexTypeL(MeasurementsS[1]).setArchIndexTypeR(MeasurementsS[2]);
                            continue;
                        }
                    }
                }
                if(s[0].equals("Hallux Angle")){
                    if(s.length==1){
                        continue;
                    }
                    for (int j=row; j < row+2; j++) {
                        // 取得第row行第0列的数据
                        String MeasurementsCell = csvFileList.get(j)[0];
                        String[] MeasurementsS = MeasurementsCell.split("\t");
                        if (MeasurementsS[0].equals("Hallux Angle")) {
                            childBo.setHalluxAngleL(MeasurementsS[1]).setHalluxAngleR(MeasurementsS[2]);
                            continue;
                        }
                        if (MeasurementsS[0].equals("Type")) {
                            childBo.setHalluxAngleTypeL(MeasurementsS[1]).setHalluxAngleTypeR(MeasurementsS[2]);
                            continue;
                        }
                    }
                }

            }for (int row = 55; row < csvFileList.size(); row++) {
                // 取得第row行第0列的数据
                String cell = csvFileList.get(row)[0];
                String[] s = cell.split("\t");

                //57-70是脚的三维坐标系
                if(s[0].equals("1st Met ")) {
                    childBo.setFirstMetXCoodinatesL(s[1]).setFirstMetYCoodinatesL(s[2]).setFirstMetZCoodinatesL(s[3]).setFirstMetXCoodinatesR(s[4]).setFirstMetYCoodinatesR(s[5]).setFirstMetZCoodinatesR(s[6]);
                    continue; }
                if(s[0].equals("5th Met ")) { childBo.setFifthMetXCoodinatesL(s[1]).setFifthMetYCoodinatesL(s[2]).setFifthMetZCoodinatesL(s[3]).setFifthMetXCoodinatesR(s[4]).setFifthMetYCoodinatesR(s[5]).setFifthMetZCoodinatesR(s[6]);continue; }
                if(s[0].equals("Heel Center ")) { childBo.setHeelCenterXCoodinatesL(s[1]).setHeelCenterYCoodinatesL(s[2]).setHeelCenterZCoodinatesL(s[3]).setHeelCenterXCoodinatesR(s[4]).setHeelCenterYCoodinatesR(s[5]).setHeelCenterZCoodinatesR(s[6]);continue; }
                if(s[0].equals("Toe Tip "))  { childBo.setToeTipXCoodinatesL(s[1]).setToeTipYCoodinatesL(s[2]).setToeTipZCoodinatesL(s[3]).setToeTipXCoodinatesR(s[4]).setToeTipYCoodinatesR(s[5]).setToeTipZCoodinatesR(s[6]);continue; }
                if(s[0].equals("Heel Tip "))   { childBo.setHeelTipXCoodinatesL(s[1]).setHeelTipYCoodinatesL(s[2]).setHeelTipZCoodinatesL(s[3]).setHeelTipXCoodinatesR(s[4]).setHeelTipYCoodinatesR(s[5]).setHeelTipZCoodinatesR(s[6]);continue; }
                if(s[0].equals("Med ForeFoot "))  { childBo.setMedForeFootXCoodinatesL(s[1]).setMedForeFootYCoodinatesL(s[2]).setMedForeFootZCoodinatesL(s[3]).setMedForeFootXCoodinatesR(s[4]).setMedForeFootYCoodinatesR(s[5]).setMedForeFootZCoodinatesR(s[6]);continue; }
                if(s[0].equals("Lat ForeFoot "))  { childBo.setLatForeFootXCoodinatesL(s[1]).setLatForeFootYCoodinatesL(s[2]).setLatForeFootZCoodinatesL(s[3]).setLatForeFootXCoodinatesR(s[4]).setLatForeFootYCoodinatesR(s[5]).setLatForeFootZCoodinatesR(s[6]);continue; }
                if(s[0].equals("Med Heel "))   { childBo.setMedHeelXCoodinatesL(s[1]).setMedHeelYCoodinatesL(s[2]).setMedHeelZCoodinatesL(s[3]).setMedHeelXCoodinatesR(s[4]).setMedHeelYCoodinatesR(s[5]).setMedHeelZCoodinatesR(s[6]);continue; }
                if(s[0].equals("Lat Heel "))   { childBo.setLatHeelXCoodinatesL(s[1]).setLatHeelYCoodinatesL(s[2]).setLatHeelZCoodinatesL(s[3]).setLatHeelXCoodinatesR(s[4]).setLatHeelYCoodinatesR(s[5]).setLatHeelZCoodinatesR(s[6]);continue; }
                if(s[0].equals("Arch "))  { childBo.setArchXCoodinatesL(s[1]).setArchYCoodinatesL(s[2]).setArchZCoodinatesL(s[3]).setArchXCoodinatesR(s[4]).setArchYCoodinatesR(s[5]).setArchZCoodinatesR(s[6]);continue; }
                if(s[0].equals("User Point 1 "))   { childBo.setUserPointOneXCoodinatesL(s[1]).setUserPointOneYCoodinatesL(s[2]).setUserPointOneZCoodinatesL(s[3]).setUserPointOneXCoodinatesR(s[4]).setUserPointOneYCoodinatesR(s[5]).setUserPointOneZCoodinatesR(s[6]);continue; }
                if(s[0].equals("User Point 2 ")) { childBo.setUserPointTwoXCoodinatesL(s[1]).setUserPointTwoYCoodinatesL(s[2]).setUserPointTwoZCoodinatesL(s[3]).setUserPointTwoXCoodinatesR(s[4]).setUserPointTwoYCoodinatesR(s[5]).setUserPointTwoZCoodinatesR(s[6]);continue; }
                if(s[0].equals("Lat Arch ")){ childBo.setLatArchXCoodinatesL(s[1]).setLatArchYCoodinatesL(s[2]).setLatArchZCoodinatesL(s[3]).setLatArchXCoodinatesR(s[4]).setLatArchYCoodinatesR(s[5]).setLatArchZCoodinatesR(s[6]);continue; }
                if(s[0].equals("Med Toe 1 ")) { childBo.setMedToeOneXCoodinatesL(s[1]).setMedToeOneYCoodinatesL(s[2]).setMedToeOneZCoodinatesL(s[3]).setMedToeOneXCoodinatesR(s[4]).setMedToeOneYCoodinatesR(s[5]).setMedToeOneZCoodinatesR(s[6]);continue; }


                if(s[0].equals("Med Arch Area")) {
                    childBo.setMedArchAreaL(s[1]).setMedArchAreaR(s[2]);
                    continue;
                }

                if(s[0].equals("Med Arch Volume")) {
                    childBo.setMedArchVolumeL(s[1]).setMedArchVolumeR(s[2]);
                    continue;
                }
                if(s[0].equals("Foot Projection Area without Toes"))  {
                    childBo.setFootAreaL(s[1]).setFootAreaR(s[2]);
                    continue;
                }
                if(s[0].equals("Arch Area Relative Height"))  childBo.setArchHeightL(s[1]).setArchHeightR(s[2]);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new MyException(40003, "CSV文件标题填写错误或对应标题的数值缺失");
        } finally {
            if(reader!=null) reader.close();
        }
        return childBo;
    }
}
